package com.ywgt.future;

import java.util.concurrent.*;

public class FutureTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //异步调用
        //1.没有返回结果
        /*CompletableFuture<Void> completableFuture=CompletableFuture.runAsync(new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }));
        System.out.println(Thread.currentThread().getName()+"1111111"+i);
        System.out.println(completableFuture.get());*/
        //2.有返回结果
        CompletableFuture<Integer> completableFuture=CompletableFuture.supplyAsync(()->{
            int i=10/0;
            return 1024;
        });
        System.out.println(Thread.currentThread().getName()+"1111111");
        //返回成功信息调用
        completableFuture.whenComplete((t,e)->{
            System.out.println(t);
            System.out.println(e);
        }).exceptionally((e)->{  //返回失败信息调用
            System.out.println(e.getMessage());
            return 500;
        });

        System.out.println(completableFuture.get());
    }
}
